package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.ISwitchedCarRecordService;
import com.mdd.admin.validate.order.SwitchedCarRecordCreateValidate;
import com.mdd.admin.validate.order.SwitchedCarRecordSearchValidate;
import com.mdd.admin.validate.order.SwitchedCarRecordUpdateValidate;
import com.mdd.admin.vo.order.SwitchedCarRecordListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.SwitchedCarRecord;
import com.mdd.common.mapper.order.SwitchedCarRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 换车记录实现类
 * @author LikeAdmin
 */
@Service
public class SwitchedCarRecordServiceImpl implements ISwitchedCarRecordService {

    @Resource
    SwitchedCarRecordMapper orderSwitchedCarRecordMapper;

    /**
     * 换车记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderSwitchedCarRecordListedVo>
     */
    @Override
    public PageResult<SwitchedCarRecordListedVo> list(PageValidate pageValidate, SwitchedCarRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<SwitchedCarRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderSwitchedCarRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:carRecordNo@car_record_no:str",
            "=:oldCarNo@old_car_no:str",
            "=:newCarNo@new_car_no:str",
            "=:operationType@operation_type:str",
            "=:oldCarStatus@old_car_status:str",
        });

        IPage<SwitchedCarRecord> iPage = orderSwitchedCarRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<SwitchedCarRecordListedVo> list = new LinkedList<>();
        for(SwitchedCarRecord item : iPage.getRecords()) {
            SwitchedCarRecordListedVo vo = new SwitchedCarRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }
    /**
     * 换车记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(SwitchedCarRecordCreateValidate createValidate) {
        SwitchedCarRecord model = new SwitchedCarRecord();
        model.setCarRecordNo(createValidate.getCarRecordNo());
        model.setOldCarNo(createValidate.getOldCarNo());
        model.setNewCarNo(createValidate.getNewCarNo());
        model.setOperationType(createValidate.getOperationType());
        model.setOldCarStatus(createValidate.getOldCarStatus());
        orderSwitchedCarRecordMapper.insert(model);
    }

    /**
     * 换车记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(SwitchedCarRecordUpdateValidate updateValidate) {
        SwitchedCarRecord model = orderSwitchedCarRecordMapper.selectOne(
                new QueryWrapper<SwitchedCarRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "换车记录数据不存在!");

        model.setId(updateValidate.getId());
        model.setCarRecordNo(updateValidate.getCarRecordNo());
        model.setOldCarNo(updateValidate.getOldCarNo());
        model.setNewCarNo(updateValidate.getNewCarNo());
        model.setOperationType(updateValidate.getOperationType());
        model.setOldCarStatus(updateValidate.getOldCarStatus());
        orderSwitchedCarRecordMapper.updateById(model);
    }

    /**
     * 换车记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        SwitchedCarRecord model = orderSwitchedCarRecordMapper.selectOne(
                new QueryWrapper<SwitchedCarRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "换车记录数据不存在!");

        orderSwitchedCarRecordMapper.delete(new QueryWrapper<SwitchedCarRecord>().eq("id", id));
    }

}
